﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using ShopB2C.Entity;
using ShopB2C.Helper;
using ShopB2C.Parameters;
using ShopB2C.ViewModels;

namespace ShopB2C.Controllers.Manager {
    /// <summary>
    /// Property
    /// </summary>
    public partial class ClassifyController : BaseController {

        public ActionResult PropertyList(string classifyParams) {
            ClassifyParam cp = Methods.FormatClassifyParams(classifyParams);
            if (cp.ClassifyId == 0) {
                throw new Exception("请选择一个分类！");
            }
            ClassifyEntity ce = _classify.GetClassifyById(cp.ClassifyId);

            this.SiteMapPathAdd("分类特性");

            int pageCount;
            List<ClassifyPropertyEntity> cpeList = _classify.GetPropertyByClassify(
                cp.ClassifyId, this.PageSize, cp.PropertyPageNumber, out pageCount);

            ClassifyParam cpPager = new ClassifyParam() {
                ModuleKey = cp.ModuleKey,
                ParentClassifyId = cp.ParentClassifyId,
                ClassifyId = cp.ClassifyId,
                ClassifyPageNumber = cp.ClassifyPageNumber,
                PropertyPageNumber = 1234554321
            };
            object pager = this.CreatePager(Url.Action("PropertyList", new {
                classifyParams = cpPager.ToString().Replace("1234554321", "{0}")
            }), pageCount, cp.PropertyPageNumber);

            cp.AutoSequence = cpeList != null
                ? this.PageSize * (cp.PropertyPageNumber - 1) + cpeList.Count + 1
                : 1;

            return View("PropertyList",
                new ClassifyPropertyViewModel() {
                    ClassifyName = ce.Title,
                    ClassifyParams = cp,
                    ClassifyPropertyList = cpeList,
                    Pager = pager
                });
        }

        public ActionResult PropertyEdit(string classifyParams) {
            ClassifyParam cp = Methods.FormatClassifyParams(classifyParams);
            ClassifyEntity ce = _classify.GetClassifyById(cp.ClassifyId);

            this.SiteMapPathAdd("分类特性", Url.Action("PropertyList", new { classifyParams = cp.ToString() }));
            this.SiteMapPathAdd("分类特性编辑");

            ClassifyPropertyEntity cpe = cp.PropertyId == 0
                ? new ClassifyPropertyEntity() { Sequence = cp.AutoSequence, IsSearch = true }
                : _classify.GetPropertyById(cp.PropertyId);

            cp.AutoSequence = 0;
            cp.PropertyId = 0;
            return View(new ClassifyPropertyViewModel() {
                ClassifyName = ce.Title,
                ClassifyProperty = cpe,
                ClassifyParams = cp
            });
        }

        [HttpPost]
        public ActionResult PropertyEdit(string classifyParams, ClassifyPropertyViewModel cpvm) {
            ClassifyParam cp = Methods.FormatClassifyParams(classifyParams);
            ClassifyPropertyEntity cpe = cpvm.ClassifyProperty;

            cpe.ClassifyId = cp.ClassifyId;
            if (cpe.PropertyId == 0) {
                _classify.PropertyAppend(cpe);
                cp.AutoSequence++;
            }
            else {
                _classify.PropertyUpdate(cpe);
                cp.AutoSequence = 0;
                cp.PropertyId = 0;
            }
            return this.RedirectToAction(
                cpe.PropertyId == 0 ? "PropertyEdit" : "PropertyList",
                new { classifyParams = cp.ToString() });
        }

        public ActionResult PropertyDelete(string classifyParams) {
            ClassifyParam cp = Methods.FormatClassifyParams(classifyParams);
            _classify.PropertyDelete(cp.DeleteIds);
            cp.PropertyId = 0;
            cp.DeleteIds = "0";

            return this.RedirectToAction("PropertyList", new { classifyParams = cp.ToString() });
        }

        [HttpPost]
        public ActionResult PropertyEditBatchSeq(string classifyParams, FormCollection fc) {
            ClassifyParam cp = Methods.FormatClassifyParams(classifyParams);
            _classify.PropertyBatchUpdateSequence(fc["cbKey"], fc["tbSequence"]);
            cp.PropertyId = 0;

            return this.RedirectToAction("PropertyList", new { classifyParams = cp.ToString() });
        }

        #region 复制特性

        public ActionResult PropertyCopy(string classifyParams) {
            ClassifyParam cp = Methods.FormatClassifyParams(classifyParams);
            List<ClassifyEntity> classifyList = _classify.GetClassifyByModuleKey(cp.ModuleKey);

            return View(new ClassifyViewModel() {
                ClassifyParams = cp,
                Classify = _classify.GetClassifyById(cp.ClassifyId),
                ClassifyList = classifyList
            });
        }

        [HttpPost]
        public ActionResult PropertyCopy(string classifyParams, FormCollection fc) {
            ClassifyParam cp = Methods.FormatClassifyParams(classifyParams);
            _classify.PropertyCopy(fc["cbKey"], cp.ClassifyId);

            return this.RedirectToAction("PropertyList", new { classifyParams = classifyParams });
        }

        public JsonResult GetPropertys(int classifyParams) {
            int pageCount;
            List<ClassifyPropertyEntity> cpeList = _classify.GetPropertyByClassify(
                classifyParams, int.MaxValue, 1, out pageCount);

            return this.Json(cpeList != null ? cpeList : new object(),
                JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetPropertyVals(int classifyParams) {
            int pageCount;
            List<ClassifyPropertyValEntity> cpveList = _classify.GetPropertyValByProperty(
                classifyParams, int.MaxValue, 1, out pageCount);

            return this.Json(cpveList != null ? cpveList : new object(),
                JsonRequestBehavior.AllowGet);
        }

        #endregion

    }
}